package com.xnfh.mine.ui.activity

import android.content.Context
import android.content.Intent
import android.os.Bundle
import android.view.View
import com.huantansheng.easyphotos.EasyPhotos
import com.huantansheng.easyphotos.callback.SelectCallback
import com.huantansheng.easyphotos.models.album.entity.Photo
import com.xnfh.common.constant.KvKey
import com.xnfh.common.manager.UserInfoManager
import com.xnfh.framework.Constants
import com.xnfh.framework.base.BaseActivity
import com.xnfh.framework.log.LogUtil
import com.xnfh.framework.utils.KVUtil
import com.xnfh.image.ImageHelper
import com.xnfh.mine.R
import com.xnfh.mine.databinding.ActivityUserinforLayoutBinding
import com.xnfh.common.utils.GlideEngine
import java.util.ArrayList

/**
 * @Description:个人资料
 * @Author: ziheng
 * @CreateDate: 2023/11/24 10:08
 * @UpdateUser: ziheng
 * @UpdateDate: 2023/11/24 10:08
 * @UpdateRemark: 更新说明
 */
class UserInforActivity : BaseActivity<ActivityUserinforLayoutBinding>(),View.OnClickListener {

    companion object{
        fun start(context: Context,bundle: Bundle? = null){
            val intent = Intent(context, UserInforActivity::class.java)
            if(bundle != null){
                intent.putExtra("bundle",bundle)
            }
            context.startActivity(intent)
        }
    }
    override fun getLayoutID(): Int = R.layout.activity_userinfor_layout

    override fun init(savedInstanceState: Bundle?) {
        mBind.userHeaderImg.setOnClickListener(this)
        setHeaderTitle(getString(R.string.user_message))
        //设置当前信息展示
        val userInfo = UserInfoManager.instance.getUserInfo()
        mBind.userName.text = userInfo?.suAccount
        val headerUrl = KVUtil.getString(KvKey.USER_HEADER)
        //如果用户头像不为空就设置展示
        if (headerUrl != null) {
            ImageHelper.instance?.loadCircleImage(
                this,
                headerUrl,
                mBind.userHeaderImg,
                R.mipmap.default_avatar_img,
                R.mipmap.default_avatar_img
            )
        }

    }

    override fun observe() {
    }

    override fun onClick(v: View?) {
        when(v){
            mBind.userHeaderImg ->{
                openPic()
            }
        }
    }

    /**
     * 打开相册选择图片
     */
    private fun openPic() {
        EasyPhotos.createAlbum(
            this,
            true,
            false,
            GlideEngine.get()
        )//参数说明：上下文，是否显示相机按钮，是否使用宽高数据（false时宽高数据为0，扫描速度更快），[配置Glide为图片加载引擎](https://github.com/HuanTanSheng/EasyPhotos/wiki/12-%E9%85%8D%E7%BD%AEImageEngine%EF%BC%8C%E6%94%AF%E6%8C%81%E6%89%80%E6%9C%89%E5%9B%BE%E7%89%87%E5%8A%A0%E8%BD%BD%E5%BA%93)
            .setFileProviderAuthority(Constants.SELECT_PIC)//参数说明：见下方`FileProvider的配置`
            .start(object : SelectCallback() {
                override fun onResult(photos: ArrayList<Photo>?, isOriginal: Boolean) {
                    if (photos != null && photos.size > 0) {
                        ImageHelper.instance?.loadCircleImage(
                            this@UserInforActivity,
                            photos[0].path,
                            mBind.userHeaderImg,
                            R.mipmap.default_avatar_img,
                            R.mipmap.default_avatar_img
                        )
                    }
                }

                override fun onCancel() {
                    LogUtil.d("取消选择图片回调")
                }

            })
    }


}